//
//  CourseVideoCell.m
//  TrainTree
//
//  Created by wanghui on 2017/9/4.
//  Copyright © 2017年 YouYeShu. All rights reserved.
//

#import "CourseVideoCell.h"
#import "CourseVideoCollectionCell.h"
#import "CourseDetailController.h"
static NSString *ID = @"CourseVideoCollectionCell";
@interface CourseVideoCell()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) NSArray *cellArray;


@end

@implementation CourseVideoCell


- (void)awakeFromNib {
    [super awakeFromNib];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(128, 93);
    flowLayout.minimumInteritemSpacing = 8;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;

    [_collectionView setCollectionViewLayout:flowLayout];
    [_collectionView registerNib:[UINib nibWithNibName:@"CourseVideoCollectionCell" bundle:nil] forCellWithReuseIdentifier:ID];

}

- (void)configWithData:(CourseListModel *)courseListModel lineColor:(UIColor *)lineColor
{
    self.cellArray = @[];
    [self.collectionView reloadData];
    
    self.cellArray = courseListModel.lessons;
    self.titleLabel.text = courseListModel.moduleName;
    self.verticalLine.backgroundColor = lineColor; 
    NSIndexPath *index = [NSIndexPath indexPathForItem:0 inSection:0];
    [self.collectionView scrollToItemAtIndexPath:index atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
}

#pragma mark - collectionView 代理方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.cellArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    CourseVideoCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        if(indexPath.row < self.cellArray.count) {
            [cell configWithModel:self.cellArray[indexPath.row]];
        }


    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LessonModel *model = self.cellArray[indexPath.row];
    if (_delegate && [_delegate respondsToSelector:@selector(jumpToCourseDetail:)]) {

        [_delegate jumpToCourseDetail:model.lessonId];

    }
}

- (NSArray *)cellArray
{
    return _cellArray ?: (_cellArray = [[NSArray alloc] init]);
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
